package com.liuanbang.saro.sarodemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.adapter.viewholder.RecyclerHeaderViewHolder;
import com.liuanbang.saro.sarodemo.adapter.viewholder.RecyclerItemViewHolder;

import java.util.List;

/**
 * main中使用的
 * 首页 可隐藏ToolBar里面的RecycleView使用RecycleViewAdapter
 * RecyclerView Adapter that allows to add a header view.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 2;
    private static final int TYPE_ITEM = 1;
    private List<String> mItemList;
    private OnRecycleViewItemClickListener onRecycleViewItemClickListener = null;
    private RecyclerView recyclerView = null;

    public RecyclerAdapter(List<String> itemList, RecyclerView recyclerView) {
        mItemList = itemList;
        this.recyclerView = recyclerView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        if (viewType == TYPE_ITEM) {
            final View view = LayoutInflater.from(context).inflate(R.layout.item_recycleview_main, parent, false);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (recyclerView != null) {
                        onRecycleViewItemClickListener.onItemClick(recyclerView.getChildAdapterPosition(view));
                    }
                }
            });
            return RecyclerItemViewHolder.newInstance(view);

        } else if (viewType == TYPE_HEADER) {
            final View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview_header, parent, false);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (recyclerView != null) {
                        onRecycleViewItemClickListener.onItemClick(recyclerView.getChildAdapterPosition(view));
                    }
                }
            });
            return new RecyclerHeaderViewHolder(view);
        }


        throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (!isPositionHeader(position)) {
            RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder;
            String itemText = mItemList.get(position - 1); // header
            holder.setItemText(itemText);
        }
    }

    public int getBasicItemCount() {
        return mItemList == null ? 0 : mItemList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position)) {
            return TYPE_HEADER;
        }

        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return getBasicItemCount() + 1;
        // header
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    /**
     * 自定义监听器接口
     */
    public interface OnRecycleViewItemClickListener {
        void onItemClick(int position);
    }

    /**
     * 自定义监听器的set方法
     * 实现RecycleView的监听,直接调用该方法 即可
     *
     * @param listener
     */
    public void setOnRecycleViewItemClickListener(OnRecycleViewItemClickListener listener) {
        onRecycleViewItemClickListener = listener;
    }
}
